package kr.ac.chungbuk;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	IBoardDAO ibaties = new IBoardDAO();
	List<userDTO> list=new ArrayList<userDTO>();
	/*ArrayList<userDTO> list=new ArrayList<userDTO>();*/
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	@RequestMapping("loginForm")
	public String loginForm(){
		return "loginForm";
	}
	
	@RequestMapping(value="loginProcess",method=RequestMethod.GET)
	public ModelAndView login(ModelAndView mav, String id, String pw,HttpServletRequest request){
		userDTO user=ibaties.selectOne(id);
		if(user==null){
			System.out.println("사용자 아이디값이 없습니다.");
			mav.setViewName("redirect:list");
		}else{
			System.out.println("id사용자아이디는: "+user);
			if(pw.equals(user.getPw())){
				request.getSession().setAttribute("user", user);
			}else{
				System.out.println("패스워드가 틀립니다.");
			}
			mav.setViewName("redirect:list");
		}
		return mav;
	}
	
	@RequestMapping("logout")
	public String logout(HttpServletRequest request){
		request.getSession().invalidate();
		return "redirect:list";
	}

	@RequestMapping(value="insertProc",method=RequestMethod.GET)
	public ModelAndView insertProc(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		String id=request.getParameter("id");
		String pw=request.getParameter("pw");
		userDTO user=new userDTO(id,pw);
		list.add(user);
		mav.addObject("userList", list);
		ibaties.insertUser(user);
		mav.setViewName("redirect:list");
				
		return mav;
	}
	
	@RequestMapping("insertUser")
	public ModelAndView insertUser(ModelAndView mav){
		mav.setViewName("main");
		mav.addObject("bodypage","insertUser.jsp");
		mav.addObject("footerpage","footer.jsp");
		return mav;
	}
	@RequestMapping("list")
	public ModelAndView listUser(ModelAndView mav, HttpServletRequest request){
		list=ibaties.selectAll();
		mav.addObject("userList",list);
		mav.setViewName("main");
		
		if(request.getSession().getAttribute("user")!=null){
			//로그인되었을때
			mav.addObject("loginpage","loginOutcome.jsp");
		}else{
			//사용자아이디없을때
			mav.addObject("loginpage","loginForm.jsp");
		}
		mav.addObject("bodypage","list.jsp");
		mav.addObject("footerpage","footer.jsp");
		
		return mav;
	}
	@RequestMapping("main")
	public String main(){
		return "main";
	}
}
